package org.edu.page;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils
        extends org.apache.commons.lang3.StringUtils
{
    public static final String EMPTY = "";
    private static Pattern linePattern = Pattern.compile("_(\\w)");

    public static boolean isNotEmpty(String str)
    {
        if ((str != null) && (!"".equals(str.trim()))) {
            return true;
        }
        return false;
    }

    public static boolean isNullOrEmpty(Object obj)
    {
        if (obj == null) {
            return true;
        }
        if ((obj instanceof CharSequence)) {
            return ((CharSequence)obj).length() == 0;
        }
        if ((obj instanceof Collection)) {
            return ((Collection)obj).isEmpty();
        }
        if ((obj instanceof Map)) {
            return ((Map)obj).isEmpty();
        }
        if ((obj instanceof Object[]))
        {
            Object[] object = (Object[])obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (int i = 0; i < object.length; i++) {
                if (!isNullOrEmpty(object[i]))
                {
                    empty = false;
                    break;
                }
            }
            return empty;
        }
        return false;
    }

    public static String lineToHump(String str)
    {
        String strTmp = str.toLowerCase();
        Matcher matcher = linePattern.matcher(strTmp);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String toStringByObject(Object obj)
    {
        return toStringByObject(obj, false, null);
    }

    public static String toStringByObject(Object obj, boolean isqdkg, String datatype)
    {
        if (obj == null) {
            return "";
        }
        if (isqdkg) {
            return obj.toString().trim();
        }
        if (hasText(datatype))
        {
            if ((obj instanceof Timestamp)) {
                return DateUtils.format((Timestamp)obj, datatype);
            }
            if ((obj instanceof Date)) {
                return DateUtils.format((Timestamp)obj, datatype);
            }
        }
        return obj.toString();
    }

    public static boolean hasText(CharSequence str)
    {
        if (!hasLength(str)) {
            return false;
        }
        int strLen = str.length();
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasLength(CharSequence str)
    {
        return (str != null) && (str.length() > 0);
    }

    public static boolean hasText(String str)
    {
        return hasText(str);
    }

    public static int parseInt(Object str)
    {
        return parseInt(str, 0);
    }

    public static int parseInt(Object str, int defaultValue)
    {
        if ((str == null) || (str.equals(""))) {
            return defaultValue;
        }
        String s = str.toString().trim();
        if (!s.matches("-?\\d+")) {
            return defaultValue;
        }
        return Integer.parseInt(s);
    }

    public static String getNextOrderNum(String sort)
    {
        int i = Integer.valueOf(sort).intValue() + 1;

        StringBuffer sb = new StringBuffer();

        String num = String.valueOf(i);
        for (int j = 0; j < 5 - num.length(); j++) {
            sb.append("0");
        }
        if (num.length() <= 5) {
            num = sb.toString() + num;
        } else {
            num = "无可用序列";
        }
        return num;
    }

    public static String getOrderPaySn()
    {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}
